package com.plian.system.dao.sys.excel;

import com.plian.Tools.pagedata.PageData;
import com.plian.system.bean.sys.SafeProdExcelBean;
import com.plian.system.dao.DaoSupport;
import com.plian.system.entity.sys.excel.AutoExcel;
import com.plian.system.model.page.Page;
import com.plian.system.vo.sys.excel.AutoExcelValueVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class AutoExcelDao {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

//    ExcelMapper
    /**
     * 保存表格
     *
     * @param pd
     * @throws Exception
     */
    public void saveAutoExcel(PageData pd) throws Exception {
        dao.save("ExcelMapper.save", pd);
    }
    public void editAutoExcel(PageData pd) throws Exception {
        dao.update("ExcelMapper.edit", pd);
    }
    /*
    * 添加数据库表
    *
    * */
    public void dropTable(@Param("tableName") String tableName) throws Exception {
        dao.save("ExcelMapper.dropTable", tableName);
    }
    public void createBill(String tableName) throws Exception {
        dao.save("ExcelMapper.createBill", tableName);
    }
    public void createValue(String tableName) throws Exception {
        dao.save("ExcelMapper.createValue", tableName);
    }
    public PageData findExcelById(String id) throws Exception {
        return (PageData)dao.findForObject("ExcelMapper.findById",id);
    }
    /**
     *删除表格
     **/
    public void delAutoExcel(List<String> list) throws Exception {
        dao.delete("ExcelMapper.deleteAll", list);
    }
    /*
    * 表格列表
    * */
    public List<PageData> listAutoExcel(Page page) throws Exception {
        return (List<PageData>) dao.findForList("ExcelMapper.datalistPage", page);
    }
    /*
     * 根据code找套表
     * */
    public PageData findByCode(PageData pd) throws Exception {
        return (PageData) dao.findForObject("ExcelMapper.findByCode", pd);
    }

    public List<PageData> findJianzhenList(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelMapper.findJianzhenList", pd);
    }


    /*
     * 根据code找套表
     * */
    public List<PageData> findListByCode(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelMapper.findListByCode", pd);
    }
//    ExcelFormMapper
    /**
     * 保存套表
     *
     * @param pd
     * @throws Exception
     */
    public void saveForm(PageData pd) throws Exception {
        dao.save("ExcelFormMapper.save", pd);
    }
    public void saveAllForm(List<PageData> list) throws Exception {
        dao.save("ExcelFormMapper.saveAll", list);
    }
    /**
     * 修改套表
     *
     * @param pd
     * @throws Exception
     */
    public void editForm(PageData pd) throws Exception {
        dao.update("ExcelFormMapper.edit", pd);
    }

    public void excelEditForm(PageData pd) throws Exception {
        dao.update("ExcelFormMapper.excelEditForm", pd);
    }
    /**
     *删除套表
     **/
    public void delForm(List<String> list) throws Exception {
        dao.delete("ExcelFormMapper.deleteAll", list);
    }
    /**
     *删除套表
     **/
    public void delLinkByBillId(PageData pd) throws Exception {
        dao.delete("ExcelLinkMapper.delete", pd);
    }
    /*
     * 套表列表
     * */
    public List<PageData> listForm(Page page) throws Exception {
        return (List<PageData>) dao.findForList("ExcelFormMapper.datalistPage", page);
    }
    /*
     * 套表列表
     * */
    public List<PageData> findByExcelId(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelFormMapper.findByExcelId", pd);
    }

    /*
     * 根据id查找
     * */
    public PageData findFormById(PageData pd) throws Exception {
        return (PageData) dao.findForObject("ExcelFormMapper.findById", pd);
    }
    //    ExcelColumnMapper
    /**
     * 保存 列表
     *
     * @param pd
     * @throws Exception
     */
    public void saveColumn(PageData pd) throws Exception {
        dao.save("ExcelColumnMapper.save", pd);
    }
    public void saveAllColumn(List<PageData> list) throws Exception {
        dao.save("ExcelColumnMapper.saveAll", list);
    }
    /**
     * 修改列表
     *
     * @param pd
     * @throws Exception
     */
    public void editColumn(PageData pd) throws Exception {
        dao.update("ExcelColumnMapper.edit", pd);
    }
    /**
     *删除列表
     **/
    public void delColumn(List<String> list) throws Exception {
        dao.delete("ExcelColumnMapper.deleteAll", list);
    }
    /*
     * 列表
     * */
    public List<PageData> listColumn(Page page) throws Exception {
        return (List<PageData>) dao.findForList("ExcelColumnMapper.datalistPage", page);
    }
    /*
     * 根据code找套表
     * */
    public List<PageData> findByFormId(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelColumnMapper.findByFormId", pd);
    }

//    ExcelBillMapper
    /**
     * 保存单据
     *
     * @param pd
     * @throws Exception
     */
    public void saveBill(PageData pd) throws Exception {
        dao.save("ExcelBillMapper.save", pd);
    }

    /*
    查询主表
     */
    public PageData findById(PageData pd) throws Exception {
        return (PageData) dao.findForObject("ExcelBillMapper.findById", pd);
    }

    /*
       查询主表
        */
    public PageData findByDate(PageData pd) throws Exception {
        return (PageData) dao.findForObject("ExcelBillMapper.findByDate", pd);
    }

    /**
     * 保存link表
     */
    public void savelink(PageData pd) throws Exception {
        dao.save("ExcelLinkMapper.save", pd);
    }

    /*
    删除link表
     */
    public void dellink(PageData pd) throws Exception {
        dao.delete("ExcelLinkMapper.delete", pd);
    }

    /*
    按formId查找colunm表
     */
    public List<PageData> findcolumn(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelColumnMapper.findByFormId", pd);
    }

    /**
     * 保存值表
     */
    public void savevalue(PageData pd) throws Exception {
        dao.save("ExcelValueMapper.savevalue", pd);
    }

    /**
     * 查询值表
     */
    public List<PageData> findvalue(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelValueMapper.findvalue", pd);
    }

    /**
     * 查询值表
     */
    public List<PageData> addvalue(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelValueMapper.addvalue", pd);
    }
    /**
     * 查询值表
     */
    public List<PageData> overvalue(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelValueMapper.overvalue", pd);
    }
    /**
     *删除列表
     **/
    public void delValue(PageData pd) throws Exception {
        dao.delete("ExcelValueMapper.deleteAll", pd);
    }
    /**
     * 查询值表
     */
    public List<PageData> floatvalue(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelValueMapper.floatvalue", pd);
    }
    /**
     * 查询值表
     */
    public List<PageData> getFinancialDate(PageData pd) throws Exception {
        PageData excel = findByCode(pd);
        pd.put("excelId",excel.getString("id"));
        pd.put("billName", "excel_bill_" + excel.getString("tableName"));
        pd.put("valueName", "excel_value_" + excel.getString("tableName"));
        return (List<PageData>) dao.findForList("ExcelValueMapper.getFinancialDate", pd);
    }

    public List<AutoExcelValueVO> getFinishData(AutoExcel autoExcel, String formId, int year, int columns, int lines) throws Exception {
        PageData pd = new PageData();
        pd.put("formId", formId);
        pd.put("year", year);
        pd.put("columns", columns);
        pd.put("lines", lines);
        pd.put("billName", "excel_bill_" + autoExcel.getTableName());
        pd.put("valueName", "excel_value_" + autoExcel.getTableName());
        return (List<AutoExcelValueVO>) dao.findForList("ExcelValueMapper.getFinishData", pd);
    }
    /*
     * 查询上年度ID
     * */
    public PageData findLinkById(PageData pd) throws Exception {
        return (PageData) dao.findForObject("ExcelLinkMapper.findLinkById", pd);
    }

    /*
     * 查询上年度ID
     * */
    public List<PageData> findLinksById(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelLinkMapper.findLinksById", pd);
    }


    /*
    上报
     */
    public void refer(PageData pd) throws Exception {
        dao.update("ExcelBillMapper.update", pd);
    }

    /**
     * 删除
     *
     * @param
     * @throws Exception
     */
    public void delete(PageData pd) throws Exception {
        dao.delete("ExcelBillMapper.deleteAll", pd);
    }


    public List<PageData> listPage(Page page) throws Exception {
        return (List<PageData>) dao.findForList("ExcelBillMapper.datalistPage", page);
    }

    public List<PageData> findByIds(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelBillMapper.findByIds", pd);
    }

    /**
     * 批量删除
     *
     * @param ArrayDATA_IDS
     * @throws Exception
     */
    public void deleteAll(String[] ArrayDATA_IDS) throws Exception {
        dao.delete("ExcelBillMapper.deleteAll", ArrayDATA_IDS);
    }

    public List<SafeProdExcelBean > findAqTotalValue(PageData pd) throws Exception {
        return (List<SafeProdExcelBean >) dao.findForList("ExcelValueMapper.findAqTotalValue", pd);
    }

    public List<PageData> dataList(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("ExcelBillMapper.dataList", pd);
    }
}
